<template>
  <div>
    <div class="echarts" ref="line" v-if="Object.keys(chartBar).length"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
export default {
  props: {
    chartBar: Object,
  },
  methods: {
    // 画折线图
    renderLine(obj) {
      //   默认值
      let defaultOptions = {
        title: "默认标题",
      };
      //   最终参数
      let finalObj = { defaultOptions, ...obj };
      let myChart = echarts.init(this.$refs.line);
      //  配置项
      let options = {
        title: {
          text: finalObj.title,
          left: 15,
          top: 15,
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          top: 15,
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: finalObj.xData,
        },
        yAxis: {
          type: "value",
        },
        // 核心数据
        series: finalObj.series,
      };
      //   绘制图表
      myChart.setOption(options);
    },
  },
  mounted() {
    this.renderLine(this.chartBar);
  },
};
</script>

<style lang="scss" scoped>
.el-button {
  margin-left: 15px;
}
.echarts {
  height: 400px;
  margin-top: 50px;
}
</style>